package com.apps.order.service.impl;

import com.apps.globals.activity.enums.ActivityType;
import com.apps.globals.activity.model.po.KupaActivity;
import com.apps.globals.activity.service.IKupaActivityService;
import com.apps.globals.global.service.IKupaGlobalService;
import com.apps.helper.UserConextHelper;
import com.apps.member.account.enums.AccountItemMinType;
import com.apps.member.account.service.IKupaAccountService;
import com.apps.member.service.IKupaMemberService;
import com.apps.order.enums.OrderStatus;
import com.apps.order.enums.PayType;
import com.apps.order.model.po.KupaVipGoodsOrder;
import com.apps.order.dao.KupaVipGoodsOrderMapper;
import com.apps.order.service.IKupaVipGoodsOrderService;
import com.apps.order.utils.OrderUtils;
import com.apps.sannotation.keys.JoinKey;
import com.apps.saspect.ThreadLock;
import com.apps.sexctption.DataException;
import com.apps.sexctption.NullException;
import com.apps.sworld.dao.impl.sDaoImpl;
import com.apps.vip.model.po.KupaVipGoods;
import com.apps.vip.service.IKupaVipGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大鹏
 * @since 2020-11-04
 */
@Service
public class KupaVipGoodsOrderServiceImpl extends sDaoImpl<KupaVipGoodsOrderMapper, KupaVipGoodsOrder> implements IKupaVipGoodsOrderService
{

    @Autowired
    private IKupaGlobalService globalService;

    @Autowired
    private IKupaActivityService activityService;

    @Autowired
    private IKupaMemberService memberService;

    @Autowired
    private IKupaVipGoodsService  vipGoodsService;

    @Autowired
    private IKupaAccountService accountService;

    /**
     * 订单前缀
     */
    private String SnPrefix="vip_";

    @Override
    public KupaVipGoodsOrder CreateOrder(Integer member_id, Integer vip_goods_id)
    {
        KupaVipGoods vipGoods=vipGoodsService.getOneById(vip_goods_id);
        if(null == vipGoods)
            throw new NullException("vip商品"+vip_goods_id+"不存在");
        //校验用户是否已经是vip
        if(memberService.isVip(member_id))
            throw new DataException("您已经是VIP,可以直接下载");
        //找出符合条件的活动
        KupaActivity activity=activityService.getOne(ActivityType.OPENVIP,vipGoods.getPrice());
        //构建订单
        KupaVipGoodsOrder vipOrder=new KupaVipGoodsOrder();
        vipOrder.setMember_id(member_id);
        vipOrder.setSn(SnPrefix+ OrderUtils.TransactionNo());
        vipOrder.setVip_goods_name(vipGoods.getName());
        vipOrder.setOrder_status(OrderStatus.WAITPAY.name());
        vipOrder.setVip_goods_id(vip_goods_id);
        vipOrder.setActivity_id(null != activity?activity.getId():null);
        vipOrder.setAmount_price(null != activity?activity.getAmount_price():new BigDecimal(0));
        //求出实付价格
        vipOrder.setPay_price(vipGoods.getPrice().subtract(vipOrder.getAmount_price()));
        return saveToDB(vipOrder);
    }

    @ThreadLock
    @Override
    public void PaySuccess(String trade_sn, @JoinKey String sn, BigDecimal price, PayType payType)
    {
        if(!sn.contains(SnPrefix))
            return;
        KupaVipGoodsOrder vipOrder=getOneByFieldValue("sn",sn);
        if(null == vipOrder)
            throw new NullException("订单"+sn+"不存在");
        if(!vipOrder.getOrder_status().equals(OrderStatus.WAITPAY.name()))
            throw new NullException("订单"+sn+"不是待付款状态");
        if(price.compareTo(vipOrder.getPay_price()) != 0)
            throw new DataException("订单"+sn+"付款金额和订单金额不一致");
        //如果是账户付款需要扣除下载币
        if(payType == PayType.ACCOUNT)
            accountService.Spend(UserConextHelper.getCurrentMember().getId(),price, AccountItemMinType.OPENVIP);
        KupaVipGoods vipGoods=vipGoodsService.getOneById(vipOrder.getVip_goods_id());
        if(null == vipGoods)
            throw new NullException("vip商品"+vipOrder.getVip_goods_id()+"不存在");
        vipOrder.setOrder_status(OrderStatus.PAYSUCESS.name());
        vipOrder.setTrade_sn(trade_sn);
        vipOrder.setPay_type(payType.name());
        updateById(vipOrder);
        memberService.openingVip(vipOrder.getMember_id(),vipGoods);
    }
}
